#include "view_sports.h"
#include "Motor.h"
#include "app_trace.h"
#include "battery_ctrl.h"
#include "blecom.h"
#include "datetime.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "font/num_font.h"
#include "heart_rate_interface.h"
#include "lfs_file_manager_auto.h"
#include "localize.h"
#include "platform_assert.h"
#include "rtc.h"
#include "sport_his.h"
#include "sports_mode.h"
#include "times/view_time_0.h"
#include "times/view_time_19.h"
#include "times/view_times.h"
#include "view.h"
#include "view_common.h"
#include "view_sports.inc"
#define LOG(fmt, ...) // app_trace_log(fmt "\r\n", ##__VA_ARGS__)

static void view_sports_base_background(void)
{
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 10, 34, FILE_IMG_function_icon);
}

void view_sport_base_sport_icon(void)
{
    disp_draw_user_image(41, 182, view_sports_base_icon);
}

void show_timer(void)
{
    int duration = disp_ctrl.com_data.i_temp[2];
    int second = duration % 60;
    duration /= 60;
    int minute = duration % 60;
    duration /= 60;
    int hour = duration % 60;
    int x = 47;
    int y = 38;
    int front_width = 9;
    int front_spacing = 1;
    int colon_width = 3;
    disp_draw_user_image(9, 35, img_sport_timer);
    disp_draw_user_image(x, y, img_sport_num_TAB[hour / 10]);
    x += (front_width + front_spacing);
    disp_draw_user_image(x, y, img_sport_num_TAB[hour % 10]);
    x += (front_width + front_spacing);
    disp_draw_user_image(x, y, img_sport_colon);
    x += (front_spacing + colon_width);
    disp_draw_user_image(x, y, img_sport_num_TAB[minute / 10]);
    x += (front_spacing + front_width);
    disp_draw_user_image(x, y, img_sport_num_TAB[minute % 10]);
    x += (front_spacing + front_width);
    disp_draw_user_image(x, y, img_sport_colon);
    x += (front_spacing + colon_width);
    disp_draw_user_image(x, y, img_sport_num_TAB[second / 10]);
    x += (front_spacing + front_width);
    disp_draw_user_image(x, y, img_sport_num_TAB[second % 10]);
}

void display_num(int number, int x, int y)
{
    int length = get_num_digit(number);
    int front_width = 9;
    int front_spacing = 1;
    x += (length - 1) * (front_width + front_spacing);
    for (int i = 0; i < length; i++)
    {
        disp_draw_user_image(x, y, img_sport_num_TAB[number % 10]);
        x -= (front_width + front_spacing);
        number /= 10;
    }
}

void record_heart_reat(void)
{
    disp_draw_user_image(13, 177, img_sport_heart_reat);
    int heart_reat = disp_ctrl.com_data.i_temp[9];
    if (heart_reat > 300)
    {
        heart_reat = 0;
    }
    display_num(heart_reat, 50, 179);
    disp_draw_user_image(79, 182, img_sport_bmp);
}

void record_distance_and_calories(const unsigned char *img_sport_icon, int record_data, const unsigned char *img_sport_unit_icon)
{
    disp_draw_user_image(12, 105, img_sport_icon);
    display_num(record_data, 50, 104);
    disp_draw_user_image(50, 123, img_sport_unit_icon);
}

void data_record(void)
{
    show_timer();
    record_heart_reat();
    int sport_type = disp_ctrl.com_data.i_temp[0];
    if (sport_type == sports_mode_mode_run_2 || sport_type == sports_mode_mode_walk || sport_type == sports_mode_mode_ride)
    {
        record_distance_and_calories(img_sport_calories, disp_ctrl.com_data.i_temp[5], img_sport_kcal);
    }
    else
    {
        record_distance_and_calories(img_sport_distance, disp_ctrl.com_data.i_temp[5], img_sport_km);
    }
}

void view_sports_base_display(void)
{

    if (disp_ctrl.loop_cnt == 0)
    {
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[9] = battery_ctrl_get_percent();
        disp_ctrl.auto_refresh_time = 500;
        disp_ctrl.com_data.i_temp[5] = ble_if_connected();
    }
    view_sports_base_background();
    view_sport_base_sport_icon();
    show_time_0_battery(disp_ctrl.com_data.i_temp[9]);
    show_time_0_ble(disp_ctrl.com_data.i_temp[5]);
}

view_event_result_t view_sports_level_1_event(int view, event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
    {
        if (view == view_sports_level_1_begin)
        {
            display_show(view_sports_level_1_end - 1, DISP_MODE_UP_MOVE);
            return view_event_result_handled;
        }
        return view_event_result_not_handled;
    }
#endif //!< EVENT_PREVIOUS_VIEW
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
    {
        if (view == view_sports_level_1_end - 1)
        {
            display_show(view_sports_level_1_begin, DISP_MODE_DOWN_MOVE);
            return view_event_result_handled;
        }
        return view_event_result_not_handled;
    }
#endif
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
    {
        display_show(view_sports_base, DISP_MODE_UP_MOVE);
        return view_event_result_handled;
    }
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
#ifdef USE_TOUCHPAD
    case BUTTON_EVENT_TP_TAP_LONG:
#endif
    {
        sports_mode_mode_t mode = sports_mode_mode_invalid;
        switch (view)
        {
        case view_sports_runing:
            mode = sports_mode_mode_run_2;
            break;
        case view_sports_walking:
            mode = sports_mode_mode_walk;
            break;
        case view_sports_cycling:
            mode = sports_mode_mode_ride;
            break;
        case view_sports_fitness:
            mode = sports_mode_mode_work_out;
            break;
        case view_sports_swing:
            mode = sports_mode_mode_swing;
            break;
        default:
            break;
        }
        sports_mode_start(mode);
        display_show(view_sports_state_0, DISP_MODE_NORMAL);
        return view_event_result_handled;
    }
    }
    return view_event_result_not_handled;
}

void view_sports_level_1_background(int view)
{
    switch (view)
    {
    case view_sports_runing:
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 17, 47, FILE_IMG_Sports_Type_run);
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 41, 183, FILE_IMG_Sports_txet_run);
    }
    break;
    case view_sports_walking:
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 17, 47, FILE_IMG_Sports_Type_walk);
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 41, 183, FILE_IMG_Sports_txet_walk);
    }
    break;
    case view_sports_cycling:
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 17, 47, FILE_IMG_Sports_Type_cycling);
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 41, 183, FILE_IMG_Sports_txet_cycling);
    }
    break;
    case view_sports_fitness:
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 17, 47, FILE_IMG_Sports_Type_fitness);
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 41, 183, FILE_IMG_Sports_txet_fitness);
    }
    break;
    case view_sports_swing:
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 17, 47, FILE_IMG_Sports_Type_swing);
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 41, 183, FILE_IMG_Sports_txet_swing);
    }
    break;
    default:
        return;
    }
}

#define POS_WIDTH 6
#define POS_INTERVAL 7
void view_sports_level_1_display(int view)
{

    if (disp_ctrl.loop_cnt == 0)
    {
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[9] = battery_ctrl_get_percent();
        disp_ctrl.com_data.i_temp[5] = ble_if_connected();
        disp_ctrl.auto_refresh_time = 500;
    }

    view_sports_level_1_background(view);
    show_time_0_battery(disp_ctrl.com_data.i_temp[9]);
    show_time_0_ble(disp_ctrl.com_data.i_temp[5]);
}
static bool is_level_1_view(int view)
{
    if (view >= view_sports_level_1_begin && view < view_sports_level_1_end)
    {
        return true;
    }
    else
    {
        return false;
    }
}

void view_sports_exit_background(void)
{
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 18, 38, FILE_IMG_back_exit);
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 44, 183, FILE_IMG_Sports_txet_exit);
}
void view_sports_level_1_back_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[9] = battery_ctrl_get_percent();
        disp_ctrl.com_data.i_temp[5] = ble_if_connected();
        disp_ctrl.auto_refresh_time = 500;
    }
    show_time_0_ble(disp_ctrl.com_data.i_temp[5]);
    show_time_0_battery(disp_ctrl.com_data.i_temp[9]);
    view_sports_exit_background();
}
void view_sports_prompt_end_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 3000;
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[9] = battery_ctrl_get_percent();
    }
    if (disp_ctrl.auto_refresh_count == 0)
    {
        show_time_0_battery(disp_ctrl.com_data.i_temp[9]);
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 26, 62, FILE_IMG_sports_icon_good_job);
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 48, 180, FILE_IMG_Sports_txet_good_job);
    }
    else
    {
        if (disp_ctrl.loop_cnt == 0)
        {
            display_show(view_sports_result_0, DISP_MODE_NORMAL);
        }
    }
}
void view_sports_error_short_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 3000;
    }
    if (disp_ctrl.auto_refresh_count == 0)
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 25, 73, FILE_IMG_sports_unsaved);
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 38, 181, FILE_IMG_Sports_txet_unsaved);
        // _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 26, 58, FILE_IMG_S514_notsave_26_58);
        // disp_show_localize(DISP_LFS_FILE_INDEX_0, localize_string_key_AlertSportNotSave, FILE_IMG_localize_en_AlertSportNotSave_26_58, 26, 58, 50);
        if (!disp_ctrl.com_data.i_temp[1])
        {
            display_show(view_sports, DISP_MODE_NORMAL);
            return;
        }
    }
    else
    {
        if (disp_ctrl.loop_cnt == 0)
        {
            display_show(view_sports, DISP_MODE_NORMAL);
        }
    }
}
/*static void show_sport_mode_text(int mode)
{
    switch (mode)
    {
    case sports_mode_mode_run_2:
    {
        // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 67, 17, FILE_IMG_S207_run_text_100_189);
        // disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportStateRun, FILE_IMG_localize_en_SportRun_100_189, 67, 17, 50);
        disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportStateRun, 50);
    }
    break;
    case sports_mode_mode_ride:
    {
        // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 67, 17, FILE_IMG_S209_cycling_text_78_189);
        // disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportStateCycling, FILE_IMG_localize_en_SportCycling_78_189, 67, 17, 50);
        disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportStateCycling, 50);
    }

    break;
    case sports_mode_mode_work_out:
    {
        // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 67, 17, FILE_IMG_S211_fitness_text_80_189);
        // disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportStateFitness, FILE_IMG_localize_en_SportFitness_80_189, 67, 17, 50);
        disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportStateFitness, 50);
    }

    break;
    case sports_mode_mode_walk:
    {
        // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 67, 17, FILE_IMG_S208_walk_text_91_189);
        // disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportStateWalk, FILE_IMG_localize_en_SportWalk_91_189, 67, 17, 50);
        disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportStateWalk, 50);
    }

    break;
    case sports_mode_mode_swing:
    {
        //_disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 67, 17, FILE_IMG_S212_swing_text_87_189);
        // disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportStateSwing, FILE_IMG_localize_en_SportSwing_87_189, 67, 17, 50);
        disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_SportStateSwing, 50);

        break;
    }
    }
}*/

void show_sport_mode_icon(int mode)
{
    //    switch (mode)
    //    {
    //    case sports_mode_mode_run_2:
    //    {
    //        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 3, 3, FILE_IMG_S401_runsmall_3_3);
    //        _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, 14, 79, FILE_IMG_time_distance_heart_rate_14_79);
    //    }
    //    break;
    //    case sports_mode_mode_ride:
    //    {
    //        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 3, 3, FILE_IMG_S403_cyclingsmall_3_3);
    //        _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, 14, 79, FILE_IMG_time_calorie_heart_rate_14_79);
    //    }
    //    break;
    //    case sports_mode_mode_work_out:
    //    {
    //        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 3, 3, FILE_IMG_S405_fienesssmall_3_3);
    //        _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, 14, 79, FILE_IMG_time_calorie_heart_rate_14_79);
    //    }
    //    break;
    //    case sports_mode_mode_walk:
    //    {
    //        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 3, 3, FILE_IMG_S402_walksmall_3_3);
    //        _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, 14, 79, FILE_IMG_time_distance_heart_rate_14_79);
    //    }
    //    break;
    //    case sports_mode_mode_swing:
    //    {
    //        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 3, 3, FILE_IMG_S406_swingsmall_3_3);
    //        _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, 14, 79, FILE_IMG_time_calorie_heart_rate_14_79);
    //    }
    //    break;
    //    }
}

void view_sports_result_0_display(void)
{

    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.com_data.i_temp[0] = sports_mode_get_now_mode();
        disp_ctrl.com_data.i_temp[2] = sports_mode_get_sports_duration(); //时间
        disp_ctrl.com_data.i_temp[3] = sports_mode_get_sports_minimal_heart_rate();
        disp_ctrl.com_data.i_temp[4] = sports_mode_get_sports_maximum_heart_rate();
        disp_ctrl.com_data.i_temp[5] = sports_mode_get_sports_calorie() / 1000;
        disp_ctrl.com_data.i_temp[6] = sports_mode_get_sports_start_time();
        if (system_config_get_km_mi() != 0)
        {
            disp_ctrl.com_data.i_temp[7] = sports_mode_get_sports_mileage() * 0.6213712;
            disp_ctrl.com_data.i_temp[8] = 0;
        }
        else
        {
            disp_ctrl.com_data.i_temp[7] = sports_mode_get_sports_mileage();
            disp_ctrl.com_data.i_temp[8] = 1;
        }
    }

    data_record();
}

//void view_sports_result_1_display(void)
//{
//#warning TODO:
//}

void animation_begin_123(int time)
{
    switch (time)
    {
    case 0:
        if (disp_ctrl.com_data.i_temp[8])
        {
            disp_ctrl.com_data.i_temp[8] = 0;
            motor_start(200);
            disp_ctrl.auto_close_delay = DISP_CLOSE_TIME_DEFAULT;
        }
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 0, 54, FILE_IMG_Sport_preparation0);
        break;
    case -1:
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 0, 54, FILE_IMG_Sport_preparation1);
        disp_ctrl.com_data.i_temp[8] = 1;
        break;
    case -2:
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 0, 54, FILE_IMG_Sport_preparation2);
        break;
    case -3:
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 0, 54, FILE_IMG_Sport_preparation3);
        break;
    }
    return;
}

void view_sports_state_0_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[0] = sports_mode_get_now_mode();
        disp_ctrl.com_data.i_temp[2] = sports_mode_get_sports_duration(); //时间
        if (hr_if_band_wear())
        {
            disp_ctrl.com_data.i_temp[9] = hr_get_hr_data();
        }
        else
        {
            disp_ctrl.com_data.i_temp[9] = 0;
        }
        disp_ctrl.com_data.i_temp[5] = sports_mode_get_sports_calorie() / 1000;
        if (system_config_get_km_mi() != 0)
        {
            disp_ctrl.com_data.i_temp[6] = sports_mode_get_sports_mileage() * 0.6213712;
            disp_ctrl.com_data.i_temp[7] = 0;
        }
        else
        {
            disp_ctrl.com_data.i_temp[6] = sports_mode_get_sports_mileage();
            disp_ctrl.com_data.i_temp[7] = 1;
        }
        if (disp_ctrl.auto_refresh_count == 0)
        {
            if (disp_ctrl.com_data.i_temp[2] < 0) //首次显示时间长一些
            {
                disp_ctrl.auto_close_delay = 10000;
            }
        }
        disp_ctrl.auto_refresh_time = 1000;
        disp_ctrl.com_data.i_temp[1] = hr_if_band_wear();
    }
    if (disp_ctrl.com_data.i_temp[2] <= 0)
    {
        disp_ctrl.auto_refresh_time = 100;
        animation_begin_123(disp_ctrl.com_data.i_temp[2]);
        return;
    }
    if (!disp_ctrl.com_data.i_temp[1])
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 23, 73, FILE_IMG_not_worn);
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 36, 181, FILE_IMG_text_not_worn);
        return;
    }
    data_record();
}

void view_sports_display(int view)
{
    if (is_level_1_view(view))
    {
        view_sports_level_1_display(view);
    }
    else
    {
        switch (view)
        {
        case view_sports_base:
            view_sports_base_display();
            break;
        case view_sports_level_1_back:
            view_sports_level_1_back_display();
            break;
        case view_sports_state_0:
            view_sports_state_0_display();
            break;
        case view_sports_result_0:
            view_sports_result_0_display();
            break;
        //case view_sports_result_1:
        //view_sports_result_1_display();
        //break;
        case view_sports_error_short:
            view_sports_error_short_display();
            break;
        case view_sports_prompt_end:
            view_sports_prompt_end_display();
        default:
            break;
        }
    }
}
view_event_result_t view_sports_level_1_back_event(event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
        display_show(view_sports_level_1_begin, DISP_MODE_DOWN_MOVE);
        return view_event_result_handled;
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
        display_show(view_sports, DISP_MODE_NORMAL);
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}

view_event_result_t view_sports_state_0_event(event_param_t const *param)
{
    int duration = sports_mode_get_sports_duration();
    if (duration <= 0)
    {
        switch (param->event)
        {
#ifdef EVENT_PREVIOUS_VIEW
        case event_move_previous_view:
#endif
#ifdef EVENT_LEVEL_UP
        case event_move_level_up:
#endif
#ifdef EVENT_LEVEL_DOWN
        case event_move_level_down:
#endif
#ifdef EVENT_NEXT_VIEW
        case event_move_next_view:
#endif
#ifdef EVENT_BUTTON_FRONT
        case event_button_front_press:
        case event_button_front_long_press:
#endif
#ifdef USE_TOUCHPAD
        case BUTTON_EVENT_TP_TAP:
        case BUTTON_EVENT_TP_TAP_LONG:
#endif
            return view_event_result_handled;
        }
    }
    switch (param->event)
    {
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
#ifdef USE_TOUCHPAD
    case BUTTON_EVENT_TP_TAP_LONG:
#endif
        sports_mode_stop();
        return view_event_result_handled;

#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
#endif
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_sports_error_short_event(event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
#endif
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
    case event_button_front_long_press:
#endif
        display_show(view_sports, DISP_MODE_NORMAL);
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_sports_prompt_end_event(event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
#endif
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
    case event_button_front_long_press:
#endif
        display_show(view_sports_result_0, DISP_MODE_NORMAL);
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_sports_result_0_event(event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
    {
        return view_event_result_handled;
    }
#endif //!< #ifdef EVENT_PREVIOUS_VIEW
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
    {
        int new_view = view_sports_prompt_end;
        display_show(new_view, DISP_MODE_DOWN_MOVE);
        return view_event_result_handled;
    }
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
#ifdef USE_TOUCHPAD
    case BUTTON_EVENT_TP_TAP_LONG:
#endif
        display_show(view_sports, DISP_MODE_NORMAL);
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_sports_result_1_event(event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
        return view_event_result_handled;
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
        display_show(view_sports_result_0, DISP_MODE_DOWN_MOVE);
        return view_event_result_handled;
#endif
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
#ifdef USE_TOUCHPAD
    case BUTTON_EVENT_TP_TAP_LONG:
#endif
        display_show(view_sports, DISP_MODE_NORMAL);
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}

view_event_result_t view_sports_event(int view, event_param_t const *param)
{
    if (is_level_1_view(view))
    {
        return view_sports_level_1_event(view, param);
    }
    else
    {
        switch (view)
        {
        case view_sports_level_1_back:
            return view_sports_level_1_back_event(param);
        case view_sports_base:
            switch (param->event)
            {
#ifdef EVENT_BUTTON_FRONT
            case event_button_front_long_press:
#endif
#ifdef EVENT_LEVEL_DOWN
            case event_move_level_down:
#endif
                display_show(view_sports_level_1_begin, DISP_MODE_DOWN_MOVE);
                return view_event_result_handled;
            }
            break;
        case view_sports_state_0:
            return view_sports_state_0_event(param);
        case view_sports_error_short:
            return view_sports_error_short_event(param);
        case view_sports_prompt_end:
            return view_sports_prompt_end_event(param);
        case view_sports_result_0:
            return view_sports_result_0_event(param);
        case view_sports_result_1:
            return view_sports_result_1_event(param);
        default:
            break;
        }
    }
    return view_event_result_not_handled;
}
